package com.example.goband.api;

import com.example.goband.Service.UserService;
import com.example.goband.common.AjaxResult;
import com.example.goband.common.AppVariable;
import com.example.goband.common.UserSessionUtils;
import com.example.goband.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;


@RestController
public class CreateRoom {
    @Resource
    private UserService userService;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Autowired
    private RedisTemplate<String, Integer> redisTemplate_int;

    //TODO设置默认值
    @GetMapping("/createroom")
    public AjaxResult Create(HttpServletRequest request,String roomId){
        //TODO需要判断房间是否已存在.并且该用户没有在任何房间中
        //判断房间号是否为空
        if (roomId == null || roomId == ""){
            return AjaxResult.fail(0000,"房间号不能为空");
        }
        Integer userid = (Integer) UserSessionUtils.getUserBySession(request).getUserId();
        try {
            redisTemplate_int.opsForHash().put(roomId, "owner", userid);
            redisTemplate_int.opsForHash().put(roomId, "type", 2);
            redisTemplate_int.opsForHash().put(roomId, "guest", 0);
            redisTemplate_int.opsForHash().put(roomId, "owner_score", 0);
            redisTemplate_int.opsForHash().put(roomId, "guest_score", 0);
            redisTemplate_int.opsForHash().put(roomId, "owner_hand", 0);
            redisTemplate_int.opsForHash().put(roomId, "guest_hand", 0);
//        redisTemplate.opsForHash().putAll();
        }catch (Exception e){
            return AjaxResult.fail(0000,"创建房间失败"+e.getMessage()+"用户"+userid);
        }
        //将roomid放到session中
        HttpSession session = request.getSession(true);
        session.setAttribute("roomid",roomId);
        Map<Object, Object> room = redisTemplate.opsForHash().entries(roomId);
        return AjaxResult.success(200,"创建成功",room);

        //清空websocketsession

    }

    @GetMapping("/joinroom")
    public AjaxResult Join (HttpServletRequest request,String roomId){
        //判断房间号是否存在
        //TODO 需要判断用户是不是自己，并且还要判断是不是已经存在guest了
         boolean is_set = redisTemplate.opsForHash().hasKey(roomId,"owner");
         if (is_set == false){
             return AjaxResult.fail(0,"房间号不存在");
         }
        //获取用户id
        HttpSession session = request.getSession(true);
        User user = (User) session.getAttribute(AppVariable.USER_SESSION_KEY);
        Integer userid = user.getUserId();
         try {
             redisTemplate_int.opsForHash().put(roomId,"guest",userid);
         }catch (Exception e){
             return AjaxResult.fail(0,e.getMessage());
         }
//        HttpSession session = request.getSession(true);
        session.setAttribute("roomid",roomId);
        Map<Object, Object> room = redisTemplate.opsForHash().entries(roomId);
        return AjaxResult.success(200,"加入成功",room);
    }
}

